package com.cody.controller.student;

import com.cody.entity.*;
import com.cody.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.*;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private PassageService passageService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private CommentService commentService;

    @GetMapping({"/index"})
    public String findAll(HttpSession session) {
        List<Teacher> teachers;
        teachers = teacherService.queryAllTeacher();
        session.setAttribute("teachers", teachers);
        List<Question> questions;
        List<Passage> passages;
        questions = questionService.queryAllQuestion();
        passages = passageService.queryAllPassage();
        List<Question> questionsByCommentSum;
        questionsByCommentSum = questionService.queryQuestionByCommentSum();
        List<String> subject;
        List<String> section;
        subject = questionService.queryQuestionByQuestionSubject();
        section = questionService.queryQuestionByQuestionSection();
        session.setAttribute("questionsByCommentSum", questionsByCommentSum);
        session.setAttribute("questions", questions);
        session.setAttribute("passages", passages);
        session.setAttribute("subject", subject);
        session.setAttribute("section", section);
        return "/student/index";
    }

    @GetMapping("/question")
    public String question(HttpSession session) {
        Student student = (Student) session.getAttribute("user");
        String studentId = student.getStudentNumber();
        List<Question> questionsByStudentId;
        questionsByStudentId = questionService.queryQuestionByStudentId(studentId);
        List<Comment> comments;
        comments = commentService.queryAllCommentByNumber(studentId);
        session.setAttribute("comments", comments);
        session.setAttribute("questionsByStudentId", questionsByStudentId);
        return "redirect:/student/home";
    }

    @GetMapping("/home")
    public String home() {

        return "/student/home";
    }

    @RequestMapping("/team")
    public String team(HttpSession session) {
      /*  List<Teacher> teachers;
        teachers = teacherService.queryAllTeacher();
        session.setAttribute("teachers",teachers);*/

        return "/student/team";
    }

    @RequestMapping("/article")
    public String article() {
        return "/student/article";
    }

    @RequestMapping("/articleDetails/{passageId}")
    String article(@PathVariable("passageId") String passageId, HttpSession session) {
        int pId = Integer.valueOf(passageId);
        Passage passage;
        passage = passageService.querySinglePassage(pId);
        session.setAttribute("passage", passage);
        return "/student/articleDetails";
    }

    @RequestMapping("/exitInfo")
    public String exitInfo() {
        return "/student/exit";
    }

    // upPwd
    @RequestMapping("/upPwd")
    public String upPwd(@RequestParam(required = false, name = "password") String password,
                        @RequestParam(required = false, name = "rePassword") String rePassword, HttpSession session) {
        if (!password.equals(rePassword)) {
            session.setAttribute("msg", "密码输入不一致");
            return "/student/set";
        } else {
            Student student = (Student) session.getAttribute("user");
            studentService.updateStudentPwd(student.getStudentNumber(), rePassword);
            session.removeAttribute("user");
            return "redirect:/login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        //清空session
        session.removeAttribute("user");

        return "redirect:/login";
    }

    @RequestMapping("questionOne/{questionId}")
    public String question(@PathVariable("questionId") Integer questionId, HttpSession session
                           )  {
        List<Comment> commentList;
        Question question;
        question = questionService.queryQuestionByQuestionId(questionId);
        List<Answer> answerList;
        answerList = answerService.queryAllAnswerByQuestionId(questionId);
        commentList = commentService.queryAllCommentByQuestionId(questionId);
        session.setAttribute("commentList", commentList);
        session.setAttribute("answerList", answerList);
        session.setAttribute("question", question);
        session.setAttribute("singleQuestionId", questionId);
        return "/question/questionDetails";
    }

    //下载
    /*@RequestMapping("questionOne/{questionId}")
    public String question(@PathVariable("questionId") Integer questionId, HttpSession session,
                           HttpServletResponse response *//*String txtName*//**//*, @RequestHeader(value = "referer", required = false) final String referer*//*) throws IOException {
        Question question1 = questionService.queryQuestionByQuestionId(questionId);
        String pathName = question1.getQuestionPicPath();
        File file = new File(pathName);
        response.reset();
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "attachment;filename=" + question1.getQuestionPicName());
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        byte[] buff = new byte[1024];
        OutputStream os = response.getOutputStream();
        int i = 0;
        while ((i = bis.read(buff)) != -1) {
            os.write(buff, 0, i);
            os.flush();
        }
        // return "redirect:/" + referer;
        return "/student/test";
    }*/
    @RequestMapping("/set")
    public String set(HttpSession session) {
        Student student = (Student) session.getAttribute("user");
        Student student1;
        student1 = studentService.querySingleStudent(student.getStudentNumber(), student.getStudentPassword());
        session.setAttribute("student", student1);
        return "/student/set";
    }

    @RequestMapping("/search")
    public String search(@RequestParam("keyContent") String keyContent, HttpSession session) {
        //关键字根据问题题目进行搜索
        List<Question> questionList;
        questionList = questionService.queryQuestionByQuestionText("%" + keyContent + "%");
        //没有搜索到问题
        String res = "共搜索到" + questionList.size() + "条数据";
        int sum;
        System.out.println("关键字："+keyContent);
        sum = questionList.size();
        session.setAttribute("keyContent", keyContent);
        session.setAttribute("searchres", res);
        session.setAttribute("sum", sum);
        session.setAttribute("questionList", questionList);
        return "/student/searchres";

    }

    @RequestMapping("/requestQuestion")
    public String requestQuestion(HttpSession session) {
        List<Teacher> teacherLists;
        teacherLists = teacherService.queryAllTeacher();
        session.setAttribute("teacherLists", teacherLists);

        return "/student/requestQuestion";
    }




    @RequestMapping("/update")
    public String update(@RequestParam(required = false, name = "username") String username,
                         @RequestParam(required = false, name = "userNumber") String userNumber,
                         @RequestParam(required = false, name = "phone") String phone,
                         @RequestParam(required = false, name = "studentSpecialty") String studentSpecialty,
                         @RequestParam(required = false, name = "studentClass") String studentClass,
                         HttpSession session) {

        Student student = new Student(1, userNumber, username, 1, "1", studentSpecialty, studentClass, phone);
        studentService.updateStudentInfo(student);
        return "redirect:/student/set";
    }
    @RequestMapping("/del")
    @ResponseBody
    public Map<String, String> del(@RequestParam(required = false, name = "studentId") int studentId) {
        Map<String, String> map = new HashMap<>();
        boolean flag =  studentService.delStudentByStudentId(studentId);
        if(flag){
            map.put("msg","删除成功");
        }
        return map;
    }

    @RequestMapping("/questionSearch")
    public String questionSearch(
            @RequestParam(required = false, name = "questionSubject") String questionSubject,
            @RequestParam(required = false, name = "questionSection") String questionSection,
            HttpSession session) {
        List<Question> AllQuestionLists;
        AllQuestionLists = questionService.queryQuestionByQuestionsubjectAndQuestionsection(questionSubject, questionSection);
        session.setAttribute("listSum", AllQuestionLists.size());
        session.setAttribute("AllQuestionLists", AllQuestionLists);
        return "/student/QuestionBySs";
    }


}
